package com.dragon.mmochat.model.deal;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.message.Mmochat_5_31S;
import com.dragon.mmochat.model.enumType.MmochatDealRoleState;
import com.dragon.mmochat.model.enumType.MmochatObjectType;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.util.MmochatUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MmochatDealPackage implements Serializable {
	private static final long serialVersionUID = 6208560023438737097L;
	private int roleId; // 交易方
	private String imei; // 交易方的imei号
	private MmochatDealRoleState dealState = MmochatDealRoleState.选择交易物品; // 交易状态
	// 交易的物品
	private Map<Integer, MmochatPackageGrid> myDealPackageGrids = new ConcurrentHashMap<Integer, MmochatPackageGrid>();
	private int small_money = 0; // 交易的钱

	public MmochatDealPackage() {
		for (int i = 0; i < MmochatConstant.dealPackageGridNum; i++) {
			MmochatPackageGrid grid = new MmochatPackageGrid();
			grid.setCanUse(true);
			grid.setGridIndex(i);
			grid.setObject(null);
			myDealPackageGrids.put(i, grid);
		}
	}

	// 返回添加成功后的格子
	public MmochatPackageGrid addObjToDealPackage(MmochatCommonObject obj) {
		if (obj == null) {
			return null;
		}
		for (MmochatPackageGrid grid : myDealPackageGrids.values()) {
			if (grid.getObject() == null) {
				grid.setObject(obj);
				return grid;
			}
		}
		return null;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public MmochatDealRoleState getDealState() {
		return dealState;
	}

	public void setDealState(MmochatDealRoleState dealState) {
		this.dealState = dealState;
	}

	public void stopDeal() {
		this.dealState = MmochatDealRoleState.中止交易;
		MmochatPlayer p = MmochatMainService.getRoleFromBuffer(roleId);
		if (p != null) {
			p.setDealId(0);
		}
	}

	public int getSmall_money() {
		return small_money;
	}

	public void setSmall_money(int smallMoney) {
		small_money = smallMoney;
	}

	public void addSmall_money(int add) {
		if ((long)Integer.MAX_VALUE - small_money < add) {
			small_money = Integer.MAX_VALUE;
		} else {
			small_money += add;
		}
		small_money = Math.max(0, small_money);
	}

	public Map<Integer, MmochatPackageGrid> getMyDealPackageGrids() {
		return myDealPackageGrids;
	}

	public void setMyDealPackageGrids(
			Map<Integer, MmochatPackageGrid> myDealPackageGrids) {
		this.myDealPackageGrids = myDealPackageGrids;
	}

	public String getImei() {
		return imei;
	}

	public void setImei(String imei) {
		this.imei = imei;
	}

	public String getDealLog() {
		MmochatDealObjs dealLog = new MmochatDealObjs();
		dealLog.setSmall_money(small_money);
		for (MmochatPackageGrid grid : myDealPackageGrids.values()) {
			MmochatCommonObject obj = grid.getObject();
			if (obj != null) {
				dealLog.getDealObjs().add(obj);
			}
		}
		Gson gson = new Gson();
		return gson.toJson(dealLog, new TypeToken<MmochatDealObjs>() {
		}.getType());
	}
}
